package sanshui.system.trade.controller;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.common.TradeType;
import sanshui.system.trade.model.TradeReportModel;
import sanshui.system.trade.model.TradeWorkerModel;
import sanshui.system.trade.service.IWorkerService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

@ApiOperation("前端接口")
@Slf4j
@Controller
@RequestMapping("/front")
public class FrontPageController {

    @Resource
    IWorkerService workerService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return"index";
    }

    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String trade(Model model) {
        List<TradeWorkerModel> modelsIn = workerService.selectWorkers(TradeType.WorkerStatus.IN);
        List<TradeWorkerModel> modelsEnd = workerService.selectWorkers(TradeType.WorkerStatus.END);
        modelsIn.addAll(modelsEnd);
        modelsIn.sort(Comparator.comparing(TradeWorkerModel::getWorkerId).reversed());
        model.addAttribute("workers", modelsIn.subList(0, Math.min(modelsIn.size(), 5)));
        return"index";
    }

    @GetMapping("/report/{workerId}")
    public String showReport(@PathVariable String workerId, Model model) {
        // 模拟报告数据
        List<TradeReportModel> reports = new ArrayList<>();
        model.addAttribute("reports", reports);
        return "report";
    }

    @GetMapping("/profit-chart")
    public String showChart(Model model) {
        // 模拟收益数据（日期和对应收益值）
        List<String> dates = Arrays.asList("2024-01", "2024-02", "2024-03", "2024-04");
        List<Double> profitsA = Arrays.asList(1500.0, 2300.5, 2240.1, 2180.3);
        List<Double> profitsB = Arrays.asList(3200.0, 3020.8, 3410.7, 3100.6);

        model.addAttribute("dates", dates);        // X轴日期数据
        model.addAttribute("profitsA", profitsA);  // 收益曲线A数据
        model.addAttribute("profitsB", profitsB);  // 收益曲线B数据
        return "profit-chart";
    }
}
